//
//  ViewController.m
//  UI22_动画
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain)UIView *myView;
@property (nonatomic, retain)UIButton *viewbtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 667, 375, 50)];
    self.myView.backgroundColor = [UIColor redColor];
    self.viewbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.viewbtn.frame = CGRectMake(10, 50, 80, 30);
    self.viewbtn.backgroundColor = [UIColor grayColor];
    [self.viewbtn addTarget:self action:@selector(viewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewbtn setTitle:@"view动画" forState:UIControlStateNormal];
    [self.view addSubview:self.viewbtn];
    [self.view addSubview:self.myView];
    self.viewbtn.selected = NO;
    
    UITextField *viewTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 90, 200, 50)];
    viewTF.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:viewTF];
    
    
    //监听键盘弹起
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
}

- (void)keyboardWillShow:(NSNotification *)notif{
    //得到键盘frame,通过键盘frame改变自定义工具栏高度
    CGRect frame = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSLog(@"frame == %@", NSStringFromCGRect(frame));
    //view动画
    //参数1:动画时长
    [UIView animateWithDuration:0.5 animations:^{
        self.myView.frame = CGRectMake(0, 627 - frame.size.height, 375, 50);
        NSLog(@"===== %@", NSStringFromCGRect(self.myView.frame));
    }];
}

- (void)viewBtnAction:(UIButton *)btn{
    
    if (self.viewbtn.selected) {
        [UIView animateWithDuration:0.5 animations:^{
            self.myView.frame = CGRectMake(0, 677, 375, 50);
            NSLog(@"===== %@", NSStringFromCGRect(self.myView.frame));
        }];
    }else{
    
    //view动画
    //参数1:动画时长
    [UIView animateWithDuration:0.5 animations:^{
        self.myView.frame = CGRectMake(0, 577, 375, 50);
        NSLog(@"===== %@", NSStringFromCGRect(self.myView.frame));
    }];
    }
    self.viewbtn.selected = !self.viewbtn.selected;
    //delay 延迟几秒执行
//    [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
//        self.myView.frame = CGRectMake(0, 667, 375, 50);
//    } completion:^(BOOL finished) {
//        
//    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
